package com.tomecode.tools.maven.settings.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.dom4j.DocumentException;

import com.tomecode.tools.maven.settings.SettingsFactory;
import com.tomecode.util.gui.Dialog;
import com.tomecode.util.gui.HideNotifiListener;
import com.tomecode.util.gui.PanelFactory;
import com.tomecode.util.gui.component.Button;

/**
 * Settings path
 * 
 * @author Frastia Tomas
 * 
 */
public final class FrmSettingsXmlPath extends Dialog {

	private static final long serialVersionUID = -691686530375650577L;

	private File lastSelectedSettings;

	private File lastSelectedRepository;

	private final JTextField txtSettings;
	private final JTextField txtRepository;
	/**
	 * close form
	 */
	private final Button bOk;

	private final Button bFindSettings;
	private final Button bFindRepository;

	private FrmSettingsXmlPath() {
		super(null, "Maven switcher config", 600, 140, false, true, true, true);
		txtSettings = new JTextField();
		txtSettings.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				enableButton();
			}
		});

		txtRepository = new JTextField();
		txtRepository.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				enableButton();
			}
		});

		bFindSettings = new Button("...", true, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				chooseFileSettings();
			}
		});
		bFindRepository = new Button("...", true, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				chooseFileRepository();
			}
		});

		bOk = new Button("Ok", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				try {
					SettingsFactory.saveSettingFile(new File(txtSettings.getText()), new File(txtRepository.getText()));
					hideMe(SettingsFactory.loadSettings());
				} catch (DocumentException ex) {
					JOptionPane.showMessageDialog(Desktop.getComponent(), "Failed load maven settings.xml: " + ex.getMessage(), "Failed load maven settings.xml", JOptionPane.ERROR_MESSAGE);
				} catch (IOException ex) {
					JOptionPane.showMessageDialog(Desktop.getComponent(), "Failed load maven settings.xml: " + ex.getMessage(), "Failed load maven settings.xml", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		addToButtonLayout(bOk);

		JPanel pSettings = PanelFactory.createBorderLayout(0, 0, 0, 0);
		pSettings.add(PanelFactory.wrapWithLabelNorm("Settings.xml file", txtSettings, 34), BorderLayout.CENTER);
		pSettings.add(PanelFactory.wrapByBorderLayout(bFindSettings, BorderLayout.NORTH), BorderLayout.EAST);
		bFindSettings.setPreferredSize(new Dimension(30, 20));
		JPanel pRepository = PanelFactory.createBorderLayout(0, 0, 0, 0);
		pRepository.add(PanelFactory.wrapWithLabelNorm("Repository directory", txtRepository, 10), BorderLayout.CENTER);
		pRepository.add(PanelFactory.wrapByBorderLayout(bFindRepository, BorderLayout.NORTH), BorderLayout.EAST);
		bFindRepository.setPreferredSize(new Dimension(30, 20));
		JPanel panelTxts = PanelFactory.createGridLayout(2, 1);

		panelTxts.add(pSettings);
		panelTxts.add(pRepository);
		panelRoot.add(PanelFactory.wrapWithTile("", panelTxts, 10, 15, 10, 15), BorderLayout.NORTH);
	}

	/**
	 * show {@link JFileChooser} for select maven settings.xml
	 */
	private final void chooseFileSettings() {
		final JFileChooser fc = (lastSelectedSettings == null) ? new JFileChooser() : new JFileChooser(lastSelectedSettings.getPath());
		fc.setMultiSelectionEnabled(false);
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		int returnVal = fc.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			lastSelectedSettings = fc.getSelectedFile().getParentFile();
			txtSettings.setText(fc.getSelectedFile().getPath());
			enableButton();
		}
	}

	/**
	 * show {@link JFileChooser} for select maven repository folder
	 */
	private final void chooseFileRepository() {
		final JFileChooser fc = (lastSelectedRepository == null) ? new JFileChooser() : new JFileChooser(lastSelectedRepository.getPath());
		fc.setMultiSelectionEnabled(false);
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int returnVal = fc.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			lastSelectedRepository = fc.getSelectedFile().getParentFile();
			txtRepository.setText(fc.getSelectedFile().getPath());
			enableButton();
		}
	}

	/**
	 * enable/disable button {@link #bOk}
	 */
	private final void enableButton() {
		if (txtSettings.getText().trim().length() == 0) {
			bOk.setEnabled(false);
			return;
		} else {
			File settings = new File(txtSettings.getText());
			if (!settings.isFile() || !settings.exists()) {
				bOk.setEnabled(false);
				return;
			}
		}
		if (txtRepository.getText().trim().length() == 0) {
			bOk.setEnabled(false);
			return;
		} else {
			File repository = new File(txtRepository.getText());
			if (!repository.isDirectory() || !repository.exists()) {
				bOk.setEnabled(false);
				return;
			}
		}
		bOk.setEnabled(true);
	}

	private FrmSettingsXmlPath(String message) {
		this();

	}

	public static final void showMe(final String message, final HideNotifiListener notifiListener) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new FrmSettingsXmlPath(message).addHideListeners(notifiListener).setVisible(true);
			}
		});
	}

}
